/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.webbapps.cormac_susilo.resources;

import edu.webbapps.cormac_susilo.core.IUserModel;
import edu.webbapps.cormac_susilo.wrappers.RunWrapper;

import java.util.List;
import java.util.NoSuchElementException;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.RollbackException;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.Status;

/**
 * REST Web Service for Run
 * POST is not managed via REST
 * @author simon, tor
 */
@RequestScoped
@Path("user/{user_id}/run")
public class RunResource {
    
    @Inject
    private IUserModel mgr;
    
    @GET
    @Produces("application/json")
    public Response getallbyUser (
            @PathParam("user_id") long user_id) {
        
        try {
            List<RunWrapper> runs = mgr.getRuns(user_id);
            GenericEntity<List<RunWrapper>> ge = new GenericEntity<List<RunWrapper>>(runs) {};
            return Response.ok(ge).build();
            
        } catch (NoSuchElementException e) {
            return Response.noContent().build();
        }
    }
    
    /*
     * @POST
     * Since we shall do servlet for upload of frames, 
     * frames and runs will get created by model in some way
     */
    
    @GET
    @Path("{run_id}")
    @Produces("application/json")
    public Response select (
            @PathParam("user_id") long user_id
            , @PathParam("run_id") long run_id) {
        
        try {
            RunWrapper r = mgr.getRun(run_id);
            return Response.ok(r).build();
            
        } catch (NoSuchElementException e) {
            return Response.noContent().build();
        }
    }
    
    @PUT
    @Path("{run_id}")
    @Produces("application/json")
    public Response update (
            @PathParam("user_id") long user_id
            , @PathParam("run_id") long run_id
            , @FormParam("trackname") String trackname) {
        
        if (trackname == null)
            return Response.status(Response.Status.BAD_REQUEST).build();
        if (trackname.isEmpty() || trackname.length() > 32)
            return Response.status(Response.Status.NOT_ACCEPTABLE).build();
        
        try {
             mgr.renameRun(run_id, trackname);    
             return Response.ok().build();
            
        } catch (RollbackException e) {
            return Response.serverError().build();
        } catch (NoSuchElementException e) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }
    
    @DELETE
    @Path("{run_id}")
    @Produces("application/json")
    public Response delete( 
            @PathParam("user_id") long user_id
            , @ PathParam("run_id") long run_id) {
        
        try {
            mgr.deleteRun(run_id);
            return Response.ok().build();
            
        } catch (RollbackException e) {
            return Response.serverError().build();
        } catch (NoSuchElementException e) {
            return Response.status(Status.NOT_FOUND).build();
        }
    }
}
